from app import db
import datetime
import json
from sqlalchemy.ext.declarative import DeclarativeMeta
from werkzeug.security import generate_password_hash, check_password_hash
from flask_login import UserMixin
from app import login
class DataTask(db.Model):
    __tablename__ = 'data_task'
    id = db.Column(db.Integer, primary_key=True)
    openid = db.Column(db.String(32),default=u'')
    env = db.Column(db.String(32),default=u'UAT')
    idcard = db.Column(db.Enum('成年',u'小于18岁',u'随机'),default=u'成年')
    phone_prefix = db.Column(db.String(11))
    bankcard = db.Column(db.Enum(u'随机分配',u'平安银行',u'工商银行',u'广发银行',u'农业银行',u'兴业银行',u'邮政银行',u'招商银行',u'中国银行',u'建设银行',u'华夏银行',u'浦发银行',u'民生银行',u'光大银行'),default=u'光大银行')
    data_num = db.Column(db.Integer,default=1)
    is_whitelist = db.Column(db.Boolean,default=False)
    whitelist_system = db.Column(db.String(32),default=u'循环贷')
    is_member = db.Column(db.Boolean, default=False)
    member_type = db.Column(db.Enum(u'随机会员',u'黄金年卡',u'白金年卡',u'钻石年卡',u'黄金季卡',u'白金季卡',u'钻石季卡',u'黄金体验卡',u'白金体验卡',u'钻石体验卡',u'信而富148黄金体验卡'),default=u'黄金年卡')
    member_system = db.Column(db.String(32),default=u'消费贷')
    is_register = db.Column(db.Boolean, default=False)
    is_bindcard = db.Column(db.Boolean, default=False)
    status = db.Column(db.INT,default=0)
    create_time = db.Column(db.DateTime, index=True, default=datetime.datetime.now)
    finish_time = db.Column(db.DateTime)

    def __repr__(self):
        return '<DataTask {}>'.format(self.id)

class DataDetail(db.Model):
    __tablename__='data_detail'
    id = db.Column(db.Integer, primary_key=True)
    env = db.Column(db.String(32), default=u'UAT')
    taskid = db.Column(db.Integer)
    crfuid = db.Column(db.String(32),default='')
    kissoid = db.Column(db.String(32),default='')
    password = db.Column(db.String(32),default='')
    name = db.Column(db.String(32),default='')
    idno = db.Column(db.String(32),default='')
    phone = db.Column(db.String(11),default='')
    bankname = db.Column(db.String(32),default='')
    bankno = db.Column(db.String(32),default='')
    is_whitelist = db.Column(db.Boolean, default=False)
    whitelist_system = db.Column(db.String(32), default=u'循环贷')
    is_member = db.Column(db.Boolean, default=False)
    member_type = db.Column(
        db.Enum(u'随机会员', u'黄金年卡', u'白金年卡', u'钻石年卡', u'黄金季卡', u'白金季卡', u'钻石季卡', u'黄金体验卡', u'白金体验卡', u'钻石体验卡',
                u'信而富148黄金体验卡'), default=u'黄金年卡')
    member_system = db.Column(db.String(32), default=u'消费贷')
    is_register = db.Column(db.Boolean, default=False)
    is_bindcard = db.Column(db.Boolean, default=False)
    status = db.Column(db.INT, default=0)
    describe = db.Column(db.TEXT,default='')
    create_time = db.Column(db.DateTime, index=True, default=datetime.datetime.now)
    extra = {}
    def __repr__(self):
        return '<data_detail {}>'.format(self.name)

class BusinessSteps(db.Model):
    __tablename__='business_step'
    id = db.Column(db.Integer, primary_key=True)
    business = db.Column(db.String(255))
    name = db.Column(db.String(255))
    type = db.Column(db.String(32))
    var1 = db.Column(db.String(32))
    var2 = db.Column(db.String(32))
    var3 = db.Column(db.String(32))
    var4 = db.Column(db.String(32))
    pre = db.Column(db.String(255))
    content= db.Column(db.TEXT)
    post = db.Column(db.String(255))
    sort = db.Column(db.Integer)

class Business(db.Model):
    __tablename__='business'
    id = db.Column(db.Integer, primary_key=True)
    business_code = db.Column(db.String(255))
    business_name = db.Column(db.String(255))
    business_extra = db.Column(db.TEXT)
    business_is_extra = db.Column(db.INT)
    business_desc = db.Column(db.TEXT)


class Admin(db.Model,UserMixin):
    __tablename__ = 'admin'
    id = db.Column(db.Integer,primary_key=True)
    username = db.Column(db.String(120),index=True,unique=True)
    password= db.Column(db.String(120))

    def set_password(self, password):
        self.password = generate_password_hash(password)

    def check_password(self, password):
        return check_password_hash(self.password, password)

    @login.user_loader
    def load_user(id):
        return Admin.query.get(int(id))

    def __repr__(self):
        return '<用户名:{}>'.format(self.username)


class AlchemyEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj.__class__, DeclarativeMeta):
            # an SQLAlchemy class
            fields = {}
            for field in [x for x in dir(obj) if not x.startswith('_') and x != 'metadata']:
                data = obj.__getattribute__(field)
                try:
                    json.dumps(data)  # this will fail on non-encodable values, like other classes
                    fields[field] = data
                except TypeError:  # 添加了对datetime的处理
                    if isinstance(data, datetime.datetime):
                        fields[field] = data.isoformat()
                    elif isinstance(data, datetime.date):
                        fields[field] = data.isoformat()
                    elif isinstance(data, datetime.timedelta):
                        fields[field] = (datetime.datetime.min + data).time().isoformat()
                    else:
                        fields[field] = None
            # a json-encodable dict
            return fields

        return json.JSONEncoder.default(self, obj)
class extra():
    type=''
    name=''
